package com.my.zidingyikongjian.view.two;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;

/**
 * 项目名称：矩形路径
 * 创建人：mwb
 * 创建时间：2018/7/10 11:45
 */

public class DrawRectCWAndCCW extends View {
    /**
     * 是否隐藏问题
     */
    private boolean isTextHidden;

    public DrawRectCWAndCCW(Context context, boolean isTextHidden) {
        super(context);
        this.isTextHidden = isTextHidden;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStrokeWidth(5); // 画笔宽度
        paint.setStyle(Paint.Style.STROKE); // 描边
        paint.setColor(Color.GREEN);

        // 先创建两个大小一样的路径
        // 第一个逆向生成
        Path CCWRectpath = new Path();
        RectF rect1 = new RectF(50, 50, 240, 200);
        CCWRectpath.addRect(rect1, Path.Direction.CCW);

        // 第二个顺向生成
        Path CWRectpath = new Path();
        RectF rect2 = new RectF(290, 50, 480, 200);
        CWRectpath.addRect(rect2, Path.Direction.CW);

        // 画出两个路径
        canvas.drawPath(CCWRectpath, paint);
        paint.setColor(Color.BLUE);
        canvas.drawPath(CWRectpath, paint);

        if (isTextHidden) {
            // 依据路径写出文字
            String text = "志不强者智不达，言不行者行不果";
            paint.setColor(Color.RED);
            paint.setTextSize(35);
            // 参数3 沿路径添加到文本起始位置的距离
            // 参数4 文本距离路径的距离
            canvas.drawTextOnPath(text, CCWRectpath, 0, 18, paint);// 逆时针生成
            canvas.drawTextOnPath(text, CWRectpath, 0, 18, paint);// 顺时针生成
        }
    }
}
